﻿using System;
using System.Windows.Forms;
using SurveySystem.Admin.Screens;
using SurveySystem.Factory;
using SurveySystem.Model;

namespace SurveySystem.Admin.Controllers
{
    public class EditSurveyController
    {
        private EditSurveyForm frm;
        private Survey survey;
        private SurveyFactory surveyFactory;

        public EditSurveyController(SurveyFactory factory, Survey survey)
        {
            this.surveyFactory = factory;
            this.survey = survey;

            frm = new EditSurveyForm();

            frm.Text = "Edit Survey";

            frm.btnSave.Click += new EventHandler(btnSave_Click);
            frm.btnEditQuestion.Click += new EventHandler(btnEditQuestion_Click);

            PopulateSurveyForm();
            PopulateQuestions();
        }

        #region " Event handlers "
        void btnEditQuestion_Click(object sender, EventArgs e)
        {
            survey.Title = frm.txtTitle.Text;

            EditSurveyQuestionsController con = new EditSurveyQuestionsController(survey);
            con.ShowDialog();

            PopulateQuestions();

        }

        void btnSave_Click(object sender, EventArgs e)
        {
           // TODO: validation

            survey.Title = frm.txtTitle.Text;
            survey.Description = frm.txtDescription.Text;
            survey.MoreInfoUrl = frm.txtMoreInfoUrl.Text;
            survey.Owner = frm.txtOwner.Text;
            survey.Closed = frm.chkClosed.Checked;
            
            // close date
            if (frm.chkNoCloseDate.Checked)
            {
                survey.NoCloseDate = true;
            }
            else
            {
                survey.NoCloseDate = false;
                survey.CloseDate = frm.dtpCloseDate.Value;
            }


            // NOTE: The questions should have already been parsed by now


            surveyFactory.UpdateSurvey(survey);

            frm.DialogResult = DialogResult.OK;
            frm.Close();
        }
        #endregion
        #region " Form refreshing and population methods "
        private void PopulateSurveyForm()
        {
            frm.txtTitle.Text = survey.Title;
            frm.txtDescription.Text = survey.Description;
            frm.txtMoreInfoUrl.Text = survey.MoreInfoUrl;
            frm.txtOwner.Text = survey.Owner;
            frm.chkClosed.Checked = survey.Closed;

            if (survey.NoCloseDate)
            {
                frm.chkNoCloseDate.Checked = true;
            }
            else
            {
                frm.chkNoCloseDate.Checked = false;
                frm.dtpCloseDate.Value = survey.CloseDate;
            }

        }
        
        private void PopulateQuestions()
        {
            TreeView tv = frm.tvQuestions;

            tv.Nodes.Clear();

            TreeNode q_node, a_node;

            foreach (Question q in survey.Questions)
            {
                q_node = new TreeNode();
                q_node.Text = q.QuestionText;
                q_node.Tag = q;

                foreach (Answer a in q.PossibleAnswers) 
                {
                    a_node = new TreeNode();
                    a_node.Text = a.AnswerText;
                    a_node.Tag = a;

                    q_node.Nodes.Add(a_node);
                }

                tv.Nodes.Add(q_node);
            }
        }
        #endregion

        public DialogResult ShowDialog()
        {
            return frm.ShowDialog();
        }


    }
}
